package com.coderush.admin.core.response;


import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description = "响应结果实体类")
public class Result<T> {

    public static final int SUCCESS = 200;

    /**
     * 返回码
     */
    @Schema(description = "业务状态码")
    private Integer code;

    /**
     * 返回消息
     */
    @Schema(description = "响应消息")
    private String message;

    /**
     * 返回数据
     */
    @Schema(description = "业务数据")
    private T data;

    /**
     * 私有化构造
     */
    private Result() {}

    /**
     *
     * 操作成功，且没有数据体
     * @return
     * @param <T>
     */
    public static <T> Result<T> buildSuccess() {

        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 操作成功，并且有数据体
     *
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> buildSuccess(T data) {

        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> buildFail(Integer code, String message) {

        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    /**
     * 返回数据
     *
     * @param code
     * @param message
     * @param body
     * @return
     * @param <T>
     */
    public static <T> Result<T> build(Integer code, String message, T body) {
        Result<T> result = new Result<>();
        result.setData(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

}